package com.example.wanandroid.activity.welcome

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.coroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Calendar
import kotlin.math.min

class ClockView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    private var centerX = 0f
    private var centerY = 0f
    private var radius = 0f
    private val paint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.STROKE
        strokeWidth = 5f
        isAntiAlias = true
        textSize = 30f
        textAlign=Paint.Align.CENTER
    }

    init {
        (context as LifecycleOwner).apply {
            var isRunning = true
            lifecycle.addObserver(object : LifecycleEventObserver {
                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                    if (event == Lifecycle.Event.ON_DESTROY) {
                        isRunning = false
                    }
                }
            })

            lifecycle.coroutineScope.launch {
                while (isRunning) {
                    delay(1000)
                    invalidate()
                }
            }
        }


//        GlobalScope.launch {
//            while (isRunning) {
//                delay(1000)
//                postInvalidate()
//            }
//        }

//        Thread {
//            while (true) {
//                Thread.sleep(1000)
//                postInvalidate()
//            }
//        }.start()
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerX = w / 2f
        centerY = h / 2f
        radius = min(w, h) / 2f * 0.8f
    }

    private fun drawDial(canvas: Canvas) {
        canvas.drawCircle(centerX, centerY, radius, paint)

        val tickLength = 5f
        for (i in 0..11) {
            val angle = (i * 30).toFloat()
            val x =
                (centerX + (radius - tickLength) * Math.cos(Math.toRadians(angle.toDouble()))).toFloat()
            val y =
                (centerY + (radius - tickLength) * Math.sin(Math.toRadians(angle.toDouble()))).toFloat()
            canvas.drawLine(x, y, x, y + tickLength, paint)
        }
    }

    private fun drawHands(canvas: Canvas) {
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        val second = calendar.get(Calendar.SECOND)

        //时针角度计算
        val hourAngle = (hour % 12 + minute / 60.0 + second / 3600.0) * 30.0 - 90.0
        val hourHandLength = radius * 0.5f
        val hourX = (centerX + hourHandLength * Math.cos(Math.toRadians(hourAngle))).toFloat()
        val hourY = (centerY + hourHandLength * Math.sin(Math.toRadians(hourAngle))).toFloat()
        canvas.drawLine(centerX, centerY, hourX, hourY, paint)

        //分针角度计算
        val minuteAngle = (minute + second / 60.0) * 6.0 - 90.0
        val minuteHandLength = radius * 0.7f
        val minuteX = (centerX + minuteHandLength * Math.cos(Math.toRadians(minuteAngle))).toFloat()
        val minuteY = (centerY + minuteHandLength * Math.sin(Math.toRadians(minuteAngle))).toFloat()
        canvas.drawLine(centerX, centerY, minuteX, minuteY, paint)

        //秒针角度计算
        val secondAngle = second * 6.0 - 90.0
        val secondHandLength = radius * 0.9f
        val secondX = (centerX + secondHandLength * Math.cos(Math.toRadians(secondAngle))).toFloat()
        val secondY = (centerY + secondHandLength * Math.sin(Math.toRadians(secondAngle))).toFloat()
        canvas.drawLine(centerX, centerY, secondX, secondY, paint)

        canvas.drawText("$hour : $minute : $second",120f,120f,paint)

    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        drawDial(canvas)
        drawHands(canvas)
    }
}